package com.water.bs.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.water.bs.pojo.DonationHistory;
import com.water.bs.service.UserDonationService;
import com.water.bs.utils.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.*;

@RestController
@CrossOrigin
public class UserDonationController {
    @Autowired
    private NumberUtil numberUtil;



    @Autowired
    private UserDonationService userdo;


    @RequestMapping("/qryMyDonation")
    public JSON qryMyDonation(@RequestParam("userid")String userid){
        System.out.println("查询者id"+userid);
        JSONObject rs = new JSONObject();
        List<Map<String, Object>> dohistorys = userdo.qryMyDonation(userid);
        //格式 createtime: "1648042402299" doid: "117535e1-59c7-4443-b23e-9be81d997cb9"
        //dostate: "0"  orgid: "18b2db22-77c5-4cb4-8f23-3aaf6f56d905"
        //point: 2673  userid: "1bd16ae4-24cb-43a8-8ada-b6cd63aeeaa0"
        for (Map<String, Object> dohistory : dohistorys) {
            List<Map<String, Object>> dodetail = userdo.qryMyDonationDetail((String) dohistory.get("doid"));
            dohistory.put("dotetail",dodetail);
        }
        rs.put("data",dohistorys);
        return rs;
    }
    @RequestMapping("/cancelDo")
    public JSON cancelDo(@RequestParam("doid")String doid){
        JSONObject rs = new JSONObject();
        System.out.println("要取消的捐赠id是"+doid);
        int i = userdo.cancelDo(doid);
        if (i>0){
            rs.put("state","0");
            rs.put("message","操作成功!");
            return rs;
        }else{
            rs.put("state","1");
            rs.put("message","操作失败!");
            return rs;
        }
    }

    @RequestMapping("/saveDoTransform")
    public JSON saveDoTransform(@RequestBody JSONObject transformInfo){
        JSONObject rs = new JSONObject();
        System.out.println("保存捐赠物流信息"+transformInfo);
//        //主要用 "createtime":,"orgname":,"dostate":"0","doexpress":"1111123890","doid":"a6a704f4-28ca-4b25-8a8c-4372b989f09a",
        int i = userdo.saveDoTransform(transformInfo);
        if (i>0){
            rs.put("state","0");
            rs.put("message","操作成功!");
            return rs;
        }else{
            rs.put("state","1");
            rs.put("message","操作失败!");
            return rs;
        }
    }



    @RequestMapping("/userDonation")
    public JSON userDonation(@RequestBody JSONObject donationInfo){
        JSONObject rs =  new JSONObject();
        System.out.println("用户的捐赠"+donationInfo);
        //用户的捐赠{"goods":[{"goodsname":"被褥","goodsnum":1}],"userid":"1bd16ae4-24cb-43a8-8ada-b6cd63aeeaa0","orgid":"18b2db22-77c5-4cb4-8f23-3aaf6f56d905"}
        Map<String,Object> donationMap =  new HashMap<>();
        donationMap.put("doid",UUID.randomUUID().toString());//设置初次捐赠 id
        donationMap.put("dostate",0);//设置初次捐赠 状态0
        //根据goods表积分 计算本次获得积分
        List<Map<String,Object>> userGoods= (List<Map<String, Object>>) donationInfo.get("goods");
        System.out.println("用户捐赠的列表"+userGoods);
        //用户捐赠的列表[{goodsname=夏装, goodsnum=1}, {goodsname=被褥, goodsnum=2}, {goodsname=测试2, goodsnum=1}, {goodsname=测试2, goodsnum=2}, {goodsname=测试2, goodsnum=3}]
        int point = numberUtil.pointCount(userGoods);
        System.out.println("获得积分"+point);
        donationMap.put("point",point);
        donationMap.put("orgid",donationInfo.get("orgid"));
        donationMap.put("userid",donationInfo.get("userid"));
        donationMap.put("createtime",System.currentTimeMillis());
        System.out.println("用户捐赠map"+donationMap);
        //用户捐赠map{dostate=0, doid=df2fedea-aead-4857-bba8-9fa81268bc3b, userid=1bd16ae4-24cb-43a8-8ada-b6cd63aeeaa0, point=3, orgid=18b2db22-77c5-4cb4-8f23-3aaf6f56d905}
        userdo.saveDohistory(donationMap); //先把捐赠id  用户id  目标机构id  以及积分存到dohistory
        Map<String,Object> dodetailmap = new HashMap<>();
        dodetailmap.put("doid",donationMap.get("doid"));
        for (Map<String, Object> userGood : userGoods) {
           dodetailmap.put("goodsname",userGood.get("goodsname"));
           dodetailmap.put("goodsnum",userGood.get("goodsnum"));
            userdo.saveDohistoryDetail(dodetailmap);
            //每个物品存到 dodetail(doid 对应以及积分存到dohistory)
        }
        return rs;

    }
    @RequestMapping("/editDo")
    public JSON editDo(@RequestBody JSONObject editInfo){
        JSONObject rs =  new JSONObject();
        System.out.println("修改捐赠内容:"+editInfo);
        //{"doid":"17442db1-fc9d-4138-b33e-252899a66ea0","goods":[{"goodsname":"书籍","goodsnum":1}]}
        ArrayList<Map<String,Object>> goods = (ArrayList<Map<String, Object>>) editInfo.get("goods");
        int i = 0; int j =0;
        for (Map<String, Object> good : goods) {
            good.put("doid",editInfo.get("doid"));
            System.out.println(good);
            if ((int)good.get("goodsnum")>0){
                i =userdo.editDo(good);
            }else{
               j= userdo.editDo2(good);
            }
        }
        List<Map<String, Object>> dodetail= userdo.qryMyDonationDetail((String) editInfo.get("doid"));
        int point = numberUtil.pointCount(dodetail);
        editInfo.put("point",point);
        int i1 = userdo.editDo3(editInfo);

        if ((i>0 || j>0) && i1>0){
            rs.put("state","0");
            rs.put("message","操作成功!");
            return rs;
        }else{
            rs.put("state","1");
            rs.put("message","操作失败!");
            return rs;
        }


    }
}
